//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    city:'guangzhou',
    citys:[{
          city:'guangzhou',
          name:'广州'
    },{
          city:'shenzhen',
          name:'深圳'
    },{
          city:'beijing',
          name:'北京'
    },{
          city:'shanghai',
          name:'上海'
    }],
    input1:{
      showTip:false,
    },
    input1Value:'',
    input1StopsLike:[],
    input2:{
      showTip:false,
    },
    input2Value:'',
    input2StopsLike:[],
    choices:[],
    oldInput1Value:'',
    oldInput2Value:''
  },
  changeCity:function(event){
   var value = event.target.dataset.value;
   this.setData({
     city:value,
     input1Value:'',
     input2Value:'',
      choices:[]
   });
   
  },
  //事件处理函数
  getDataFromNetwork:function(url,data,callback){
    wx.request({
      url: url,
      data:data,
      header: {
          'content-type': 'application/json'
      },
      success: function(res) {
       callback(res);
      },
      fail:function(res){
        console.log(res);
      }
    });
  },
  
  bindInput1: function(event) {
   console.log(event);
   var value = event.detail.value;
    var url="https://www.tongquxiang.com/busapi/stoplike";
    var that = this;
    var data ={
      city:this.data.city,
      stoplike:value,
    };
    this.setData({
      input1Value:value
    });
    this.getDataFromNetwork(url,data,function(res){
      var data = res.data;
      if(data.length == 0)return;
      that.setData({
        input1StopsLike:data,
        input1:{
        showTip:true
        }
      });
    });
  
  },
  bindInput2: function(event) {
   console.log(event);
   var value = event.detail.value;
    var url="https://www.tongquxiang.com/busapi/stoplike";
    var that = this;
    var data ={
      city:this.data.city,
      stoplike:value,
    };
      this.setData({
      input2Value:value
    });
    this.getDataFromNetwork(url,data,function(res){
      var data = res.data;
      if(data.length == 0)return;
      that.setData({
        input2StopsLike:data,
         input2:{
          showTip:true
        }
      });
    });
  
  },
  changeInput1: function(event) {
    var value = event.target.dataset.value;
   this.setData({
     input1:{
       showTip:false,
     },
     input1Value:value
   })
  },
   changeInput2: function(event) {
    var value = event.target.dataset.value;
   this.setData({
     input2:{
       showTip:false,
     },
     input2Value:value
   })
  },
 queryBus:function(){
    var url = "https://www.tongquxiang.com/busapi/transfer";
    var that = this;
    if(this.data.input1Value == '') {
      this.showModal('请输入起点');
      return;
    }
    if(this.data.input2Value == '') {
      this.showModal('请输入终点');
      return;
    }
    var data={
      start:that.data.input1Value,
      end:that.data.input2Value,
      city:that.data.city
    }
    this.showToast();
    this.getDataFromNetwork(url,data,function(res){
      that.hideToast();
      var data = res.data;
      var zhida = data['zhida'];
      var yicihuancheng = data['yicihuancheng'];
      var ercihuancheng = data['ercihuancheng'];
      if(yicihuancheng==undefined)yicihuancheng=[];
      if(ercihuancheng==undefined)ercihuancheng=[];
      var choices = zhida.concat(yicihuancheng).concat(ercihuancheng).slice(0,7);
      that.setData({
        oldInput1Value:that.data.input1Value,
        oldInput2Value:that.data.input2Value,
        choices:choices
      });
      // console.log(data);
      // console.log(choices);
      // that.setData({
      //   input2StopsLike:data,
      //    input2:{
      //     showTip:true
      //   }
      // });
    });
  },
  redirectToLinePage:function(event){
    var value = event.target.dataset.value;
    var that = this;
    var line = {
      city:that.data.city,
      line:value
    }
    try {
        wx.setStorageSync('line', JSON.stringify(line));
    } catch (e) {    
    }
    wx.switchTab({url: '/pages/line/index'});
  },
  redirectToStopPage:function(event){
    var value = event.target.dataset.value;
    var that = this;
    var stop = {
      city:that.data.city,
      stop:value
    }
    try {
        wx.setStorageSync('stop', JSON.stringify(stop));
    } catch (e) {    
    }
    wx.switchTab({url: '/pages/stop/index'});
  },
  showToast:function(){
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    });
  },
  hideToast:function(){
    wx.hideToast();
  },
  showModal:function(msg){
    wx.showModal({
      title: '提示',
      content: msg,
      showCancel:false,
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定')
        }
      }
    })
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this
    //调用应用实例的方法获取全局数据
    // app.getUserInfo(function(userInfo){
    //   //更新数据
    //   that.setData({
    //     userInfo:userInfo
    //   })
    // });
    
  },
   onShareAppMessage: function () {
     var that = this;
      
    return {
      title: '197',
      path: '/pages/line/index?line=197路上行&city=guangzhou',
      success: function(res) {
        that.showModal('分享成功');
      },
      fail: function(res) {
        // 分享失败
        that.showModal('分享失败');
      }
    }
  }
})
